$(function() {
	$.scrollTo($('.panel-error'));
	
	$('select.product_status').parents('table').find('input[type="button"]').attr('disabled', 'disabled');
	$('select.product_status').change(function() {
		var form = $(this).parents('form');
		var bookingFields = form.find('.ordered-items').find('.fields');
		var v = $(this).val();
		var vc = $(this).siblings('.current-status').val();
		var submitButton = $(this).parents('table').find('input[type="button"]');
		
		if (v == 5 && vc != 5) {
			bookingFields.removeAttr('disabled');
			submitButton.attr('disabled', 'disabled');
		} else if (v != 5 && vc != 5) {
			bookingFields.attr('disabled', 'disabled');
			submitButton.removeAttr('disabled');
		} else if (v == 5 && vc == 5) {
			submitButton.attr('disabled', 'disabled');
		} else {
			bookingFields.attr('disabled', 'disabled');
			submitButton.removeAttr('disabled');
		};

		if (v == 5 && $(this).parents('table').next('table.ordered-items').length <= 0) {
			submitButton.removeAttr('disabled');
		}

		if (v == -1) {
			submitButton.attr('disabled', 'disabled');
		}
		
		if(v == 7)
		{
			form.find('.orderItemCancelPenalty').fadeIn();
		}
		else
		{
			form.find('.orderItemCancelPenalty').fadeOut();
		}
	});

	$('.ordered-items .fields').change(change);
	$('.ordered-items .fields').keyup(change);
	function change(e) {
		var $this = $(this);
		var mass = [];
		var bool = true;
		$this.parents('.ordered-items').find('.required-field').each(function() {
			mass.push($(this));
		});
		for (var i = 0; i < mass.length; i++) {
			if (mass[i].val().replace(/^[\s\t\r\n]+/g, '').replace(/[\s\t\r\n]+$/g, '') == '') {
				bool = false;
			}
		}
		if (bool) {
			$this.parents('form').find('.product-status input[type="button"]').removeAttr('disabled');
		} else {
			$this.parents('form').find('.product-status input[type="button"]').attr('disabled', 'disabled');
		}
	}

	$('.product-status input[type="button"]').click(function() {
		var form = $(this).parents('form');
		var status = form.find('.product_status').val();
		if (!(status > -1))
		{
			alert('CHOOSE_STATUS'.translate());
		}
		else
		{
			var penalty = form.find('input[name="penalty"]').val().replace(/\s+/, '');
			if (status == 7 && !(penalty != '' && penalty == parseInt(penalty) && parseInt(penalty) >= 0))
			{
				alert('FILL_PENALTY'.translate());
			}
			else if(confirm('ARE_YOU_SURE_CHANGE_STATUS'.translate()))
			{
				form.submit();
			}
		}
			
	});

	$('select.product_status').each(function() {
		var $this = $(this);
		if ($this.val() == 5) {
			$this.parents('table').find('input[type="button"]').attr('disabled', 'disabled');
			$this.parents('form').find('.ordered-items .fields').attr('disabled', 'disabled');
		}
	});

	$('.insurance').click(function() {
		var $this = $(this);
		var url = $this.next('input[type="hidden"].insurance-url').val();
		var frame = $this.parents('.services').next('.insurance-iframe');
		if (frame.length <= 0) {
			var iframe = '<iframe class="insurance-iframe" src="' + url + '" height="1000" width="100%"></iframe>';
			$this.parents('.services').after(iframe);
		} else {
			if (frame.is(':visible'))
				frame.slideUp();
			else
				frame.slideDown();
		}
	});

	if ($('#edit_order').val() == 0) {
		$('input, select, textarea, button').attr('disabled', 'disabled');
	}

	$('#order-manager-text #a-order-manager').click(function() {
		$('#order-manager-text').hide();
		$('#order-manager-form').show();
	});
	$('#cancel-order-manager').click(function() {
		$('#order-manager-form').hide();
		$('#order-manager-text').show();
	});
	$('form#order-manager-form').submit(function() {
		if ($('#select-order-manager').val() == 0) {
			alert('SELECT_MANAGER'.translate());
			return false;
		}
	});

	$('#payment-status').change(function() {
		if ( $(this).val() == 'WARRANTY_LETTER' ) {
			var note = 'NOTE_MAX_FILE_SIZE'.translate();
			note = note.replace(/^(.+?)%(.*)$/, '$1' + $('#maxFileSize').val() + '$2')
			$('#garantLetterHeader').html('<strong>' + 'GARANT_LETTER_SCAN_HEADER'.translate() + '</strong>');
			$('#garantLetter').html('<input type="file" name="guarantee" /><div class="notes">' + note + '</div>').parent().show();
		} else {
			$('#garantLetterHeader').html('&nbsp;');
			$('#garantLetter').html('&nbsp;').parent().hide();
		}
	});

	$('#formPaymentStatus').submit(function() {
		if ($('#payment-status').val() == 'PERSONAL_LIABILITY') {
			return confirm('SURE_FINANCIAL_RESPONSIBILITY'.translate());
		}
		return true;
	});

	$('#manual-booking-frm').submit(function() {
		var _continue = true, price = $('#mb_price_net_vendor').val();
		$(this).find('input[type="text"], select').each(function() {
			if ($(this).val() == '') _continue = false;
		});
		
		if (!_continue) {
			alert('FILL_FIELDS'.translate());
			return false;
		} else if (price.match(/^\d+[\.,]?\d*$/) == null || price < 0) {
			alert('INPUT_CORRECT_NUMBER'.translate());
			$('#mb_price_net_vendor').focus(function() { $(this).select(); }).focus();
			return false;
		}
		var
			aDate = $('#mb_arrival_date').datepicker('getDate'),
			dDate = $('#mb_departure_date').datepicker('getDate')
		;
		if (aDate.getTime() - dDate.getTime() > 0) {
			alert('DATES_ARRIVE_COMPARE'.translate());
			return false;
		}
	});

	var ajaxLoaderImg = new Image();
	ajaxLoaderImg.src = "/i/ajax-loader-small.gif";
	var ajaxLoader = '<img id="ajaxLoader" src="/i/ajax-loader-small.gif" width="16" height="16" alt="" />';

	$('#problematic_status').click(function() {
		$this = $(this);
		$this.next('a').after(ajaxLoader);
		$.ajax({
			async   : true,
			type    : "GET",
			url     : "/a_admin/order.changeProblematicStatus",
			dataType: "json",
			data    : {status: this.checked, order_id: $('#order_id').val()},
			success : function(data){
				$('#ajaxLoader').remove();
			},
			error : function() {
				$('#ajaxLoader').remove();
			}
		});
	});

	var today = new Date();
	$('#mb_arrival_date, #mb_departure_date').datepicker({
		showOn: 'button',
		duration: '',
		speed: '',
		numberOfMonths: 2,
		prevText: '&#x3c;&#x3c;',
		nextText: '&#x3e;&#x3e;',
		currentText: 'CURRENT_TEXT'.translate(),
		firstDay: 1,
		changeFirstDay: false,
		dayNamesMin: ['DOW_SHORT_SUN'.translate(),'DOW_SHORT_MON'.translate(),'DOW_SHORT_TUE'.translate(),'DOW_SHORT_WED'.translate(),'DOW_SHORT_THI'.translate(),'DOW_SHORT_FRI'.translate(),'DOW_SHORT_SAT'.translate()],
		monthNames: ['MONTH_LONG_JAN'.translate(), 'MONTH_LONG_FEB'.translate(), 'MONTH_LONG_MAR'.translate(), 'MONTH_LONG_APR'.translate(), 'MONTH_LONG_MAY'.translate(), 'MONTH_LONG_JUN'.translate(), 'MONTH_LONG_JUL'.translate(), 'MONTH_LONG_AUG'.translate(), 'MONTH_LONG_SEP'.translate(), 'MONTH_LONG_OCT'.translate(), 'MONTH_LONG_NOV'.translate(), 'MONTH_LONG_DEC'.translate()],
		buttonImage: '/i/calendar.gif',
		buttonImageOnly: true,
		buttonText: 'CALENDAR'.translate(),
		dateFormat: 'dd.mm.yy',
		displayClose: true,
		displayCurrent: true,
		closeText: 'x',
		onClose: function() {
			if ($.browser.msie && $.browser.version.number < 7) {
				$('div.form_search_outer select').css('visibility', 'visible');
			}
		},
		minDate: today
	});
	
});